home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ultra250.zip / SHADOW.C < prev    next >
Text File  |  1992-11-12  |  10KB  |  251 lines

  1. /****************************************************************************/
  2. /*                                                                                                                                                    */
  3. /* SHADOW.C                                                                                                                                 */
  4. /*                                                                                                                                                    */
  5. /* This program show how to use UltraWin with those pretty 3-d shadowed         */
  6. /* windows everyone likes to do.  Just take the routines you want below,    */
  7. /* and put them into your own programs!                                     */
  8. /*                                                                          */
  9. /*                                                         Boyd Gafford            */
  10. /*                                                         EnQue Software        */
  11. /*                                                         02/25/92         */
  12. /*                                                                                                                                                    */
  13. /****************************************************************************/
  14. #include <ctype.h>
  15. #include "uw.h"
  16. #include "uw_globx.h"
  17. #include "uw_keys.h"
  18.  
  19.  
  20. #define    SH_TOP_LEFT            0                                /* used by shadow window functions    */
  21. #define SH_TOP_RIGHT        1
  22. #define SH_BOTTOM_RIGHT    2
  23. #define    SH_BOTTOM_LEFT    3
  24.  
  25. #define    NUM_WINDOWS         15                            /* for the popup shadow demo                */
  26.  
  27. /*----------------------- global window variables --------------------------*/
  28. WINDOW    Desk_window;                         /* global window for "desktop"         */
  29. WINDOW    Test_window;                                        /* global POPUP window for demo            */
  30. WINDOW    Win_array[NUM_WINDOWS];
  31.  
  32. char        *Test_str =
  33. "Now is the time for all good men to come to the aid of their country.  ";
  34.  
  35. /*------------------------------ prototypes --------------------------------*/
  36. void shadow_window( WINDOW *wnp, int type, int w, int h, int att );
  37. void unshadow_window( WINDOW *wnp );
  38.  
  39.  
  40. /*********/
  41. /* ~main */
  42. /*       ********************************************************************/
  43. /* This is the main function, which does the init stuff and demos the new   */
  44. /* shadow routines.                                                         */
  45. /****************************************************************************/
  46. int main()
  47. {
  48.     WINDOW    *desk_wnp = &Desk_window;
  49.     WINDOW    *test_wnp = &Test_window;
  50.     int         i, xs, ys, xe, ye, width, height;
  51.  
  52.     /*---------------------------- init stuff --------------------------------*/
  53.     init_video(80, 25);                                                        /* try an 80xe5 screen            */
  54.     init_clock(0x3333);                                                        /* init to 91 ticks/second    */
  55.     init_mouse();                                                                    /* init the mouse                        */
  56.     wn_create(0, 0, V_cols - 1, V_rows - 1,                /* one full-screen window        */
  57.                         NO_BDR, WN_POPUP, desk_wnp);
  58.     wn_color(BLACK, BLUE, desk_wnp);
  59.     wn_bdr_color(BLACK, BLUE, desk_wnp);
  60.     desk_wnp->scroll = OFF;                                                /* turn off desktop scroll    */
  61.     wn_set(desk_wnp);                                                            /* put the window on-screen    */
  62.     for (i=0; i<222; i++)                                                    /* do a little background        */
  63.     {                                                                                            /* to show off the shadow        */
  64.         wn_color((i % 8) + 8, BLUE, desk_wnp);
  65.         wn_st("UltraWin ", desk_wnp);
  66.     }
  67.     wn_color(BLACK, BLUE, desk_wnp);
  68.  
  69.  
  70.     /*---------------------- first, a simple demo ----------------------------*/
  71.     wn_create(7, 10, 72, 15,                                            /* create the demo window        */
  72.                         SGL_BDR, WN_POPUP, test_wnp);
  73.     wn_color(WHITE, LIGHTGRAY, test_wnp);
  74.     wn_bdr_color(BLACK, LIGHTGRAY, test_wnp);
  75.     wn_name(" Press any key ", test_wnp);
  76.  
  77.     wn_set(test_wnp);                                                            /* put the window on-screen    */
  78.     wn_plst(CENTERED, 2,
  79.         "Window without a shadow.", test_wnp);
  80.     wait_event();
  81.  
  82.     mv_cs(0, 2, test_wnp);                                                /* show the 4 shadow types    */
  83.     wn_cleol(test_wnp);
  84.     wn_plst(CENTERED, 2,
  85.         "Low shadowed window.", test_wnp);
  86.     for (i=SH_TOP_LEFT; i<=SH_BOTTOM_LEFT; i++)
  87.     {
  88.         shadow_window(test_wnp, i, 2, 1, DARKGRAY);
  89.         wait_event();
  90.         unshadow_window(test_wnp);
  91.     }
  92.  
  93.     mv_cs(0, 2, test_wnp);                                                /* show w and h variances        */
  94.     wn_cleol(test_wnp);
  95.     wn_plst(CENTERED, 2,
  96.         "High shadowed window.", test_wnp);
  97.     for (i=SH_TOP_LEFT; i<=SH_BOTTOM_LEFT; i++)
  98.     {
  99.         shadow_window(test_wnp, i, 4, 2, DARKGRAY);
  100.         wait_event();
  101.         unshadow_window(test_wnp);
  102.     }
  103.  
  104.     wn_destroy(test_wnp);                                                    /* destroy the window                */
  105.  
  106.     /*----------------------- now, lets go crazy! ----------------------------*/
  107.     for ( i = 0; i < NUM_WINDOWS; i++ )
  108.     {
  109.         width = random(20) + 15;
  110.         height = random(3) + 4;
  111.         xs = random(V_cols - width - 1);
  112.         ys = random(V_rows - height - 1);
  113.         xe = xs + width - 1;
  114.         ye = ys + height - 1;
  115.         wn_create(xs, ys, xe, ye, SGL_BDR, WN_POPUP, &Win_array[i]);
  116.         wn_color((i % 8) + 8, i % 8, &Win_array[i]);
  117.         wn_bdr_color((i % 8) + 8, i % 8, &Win_array[i]);
  118.     }
  119.     for ( i = 0; i < NUM_WINDOWS; i++ )
  120.     {
  121.         wn_set(&Win_array[i]);
  122.         shadow_window(&Win_array[i], SH_BOTTOM_RIGHT, 2, 1, DARKGRAY);
  123.         wn_st(Test_str, &Win_array[i]);
  124.         wn_st(Test_str, &Win_array[i]);
  125.     }
  126.     wait_event();
  127.     for( i = NUM_WINDOWS - 1; i >= 0; i-- )
  128.     {
  129.         unshadow_window(&Win_array[i]);
  130.         wn_destroy(&Win_array[i]);
  131.     }
  132.  
  133.     /*------------------------- termination stuff ----------------------------*/
  134.     wn_destroy(desk_wnp);                                                    /* destroy the desk window    */
  135.     end_mouse();                                                                    /* terminate the mouse            */
  136.     end_video();                                                                    /* back to old video mode        */
  137.     return(1);                                                                        /* exit the progarm                    */
  138. }
  139. /*** end of main ***/
  140.  
  141. /******************/
  142. /* ~shadow_window */
  143. /*                ***********************************************************/
  144. /* This routine takes the window passed by pointer and shadows it.  It does */
  145. /* this by creating sub windows using the usr_ptr member of the window      */
  146. /* structure.                                                               */
  147. /*                                                                          */
  148. /* The contents under these sub windows are read off the screen and put         */
  149. /* inside the windows.  The attribute is then changed and the window is            */
  150. /* placed on the screen, giving the illusion of a shadow.                   */
  151. /*                                                                          */
  152. /* You may pass this routine a width, height and attribute (DARKGRAY on          */
  153. /* BLACK with a width of 2 and height of 1 usually looks nice).             */
  154. /* You may also pass a type, which is a number from 0-3 (see #defines) that */
  155. /* indicates the direction the shadow falls.                                */
  156. /*                                                                          */
  157. /* The main thing to remember is be SURE that you call the unshadow_window    */
  158. /* function BEFORE you destroy the window, or else you will have heap              */
  159. /* problems.                                                                                     */
  160. /****************************************************************************/
  161. void shadow_window( WINDOW *wnp, int type, int w, int h, int att )
  162. {
  163.     WINDOW     *wnp1, *wnp2;                                                    /* shadow window pointers        */
  164.     int            xs = wnp->pane.x_min;                                    /* get the screen coords        */
  165.     int            ys = wnp->pane.y_min;
  166.     int            xe = wnp->pane.x_max;
  167.     int            ye = wnp->pane.y_max;
  168.     int            win1_xs, win1_ys, win1_xe, win1_ye;
  169.     int            win2_xs, win2_ys, win2_xe, win2_ye;
  170.     int            i;
  171.     uchar        *dest;
  172.  
  173.     if (wnp->usr_ptr == NULL)
  174.     {
  175.         switch(type)
  176.         {
  177.             case SH_TOP_LEFT:
  178.                 win1_xs = xs - w, win1_ys = ys - h;            /* left side of window             */
  179.                 win1_xe = xs - 1, win1_ye = ye - h;
  180.                 win2_xs = xs    , win2_ys = ys - h;            /* top side of window                */
  181.                 win2_xe = xe - w, win2_ye = ys - 1;
  182.                 break;
  183.             case SH_TOP_RIGHT:
  184.                 win1_xs = xe + 1, win1_ys = ys - h;            /* right side of window             */
  185.                 win1_xe = xe + w, win1_ye = ye - h;
  186.                 win2_xs = xs + w, win2_ys = ys - h;            /* top side of window                */
  187.                 win2_xe = xe    , win2_ye = ys - 1;
  188.                 break;
  189.             case SH_BOTTOM_LEFT:
  190.                 win1_xs = xs - w, win1_ys = ys + h;            /* left side of window             */
  191.                 win1_xe = xs - 1, win1_ye = ye + h;
  192.                 win2_xs = xs    , win2_ys = ye + 1;            /* bottom side of window        */
  193.                 win2_xe = xe - w, win2_ye = ye + h;
  194.                 break;
  195.             default:
  196.                 win1_xs = xe + 1, win1_ys = ys + h;            /* right side of window             */
  197.                 win1_xe = xe + w, win1_ye = ye + h;
  198.                 win2_xs = xs + w, win2_ys = ye + 1;            /* bottom side of window        */
  199.                 win2_xe = xe    , win2_ye = ye + h;
  200.                 break;
  201.         }
  202.         wnp->usr_ptr = calloc(1, sizeof(WINDOW));        /* alloc/create first win        */
  203.         wn_create(win1_xs, win1_ys, win1_xe, win1_ye,
  204.                             NO_BDR, WN_POPUP,
  205.                             (WINDOW *) wnp->usr_ptr);
  206.         wnp1 = (WINDOW *) wnp->usr_ptr;
  207.         wn_read(wnp1);
  208.         dest = wnp1->buff + 1;                                            /* set to DARKGRAY on BLACK    */
  209.         for (i=0; i<(wnp1->cols * wnp1->rows); i++)
  210.             *dest = att, dest += 2;
  211.  
  212.         wnp1->usr_ptr = calloc(1, sizeof(WINDOW));    /* alloc/create second win    */
  213.         wn_create(win2_xs, win2_ys, win2_xe, win2_ye,
  214.                             NO_BDR, WN_POPUP,
  215.                             (WINDOW *) wnp1->usr_ptr);
  216.         wnp2 = (WINDOW *) wnp1->usr_ptr;
  217.         wn_read(wnp2);
  218.         dest = wnp2->buff + 1;                                            /* set to DARKGRAY on BLACK    */
  219.         for (i=0; i<(wnp2->cols * wnp2->rows); i++)
  220.             *dest = att, dest += 2;
  221.  
  222.         wn_save(wnp1), wn_rfsh(wnp1);                                /* do the wn_set equivalent    */
  223.         wn_save(wnp2), wn_rfsh(wnp2);
  224.     }
  225. }
  226. /*** end of shadow_window ***/
  227.  
  228. /********************/
  229. /* ~unshadow_window */
  230. /*                  *********************************************************/
  231. /*  This is the complement to shadow_window.  You MUST call this function   */
  232. /* before you destroy the window that has been shadowed or you will have        */
  233. /* severe problems.  If you don't it will have the same effect as not             */
  234. /* calling win_destroy on a created window.  Your computer will lockup!            */
  235. /****************************************************************************/
  236. void unshadow_window( WINDOW *wnp )
  237. {
  238.     WINDOW     *wnp1, *wnp2;                                                    /* shadow window pointers        */
  239.  
  240.     if (wnp->usr_ptr)                             /* does it have a shadow?        */
  241.     {
  242.         wnp1 = (WINDOW *) wnp->usr_ptr;                            /* get the window pointers    */
  243.         wnp2 = (WINDOW *) wnp1->usr_ptr;
  244.         wn_destroy(wnp2), wn_destroy(wnp1);
  245.         free(wnp2), free(wnp1);                                            /* and free the structure        */
  246.         wnp->usr_ptr = NULL;
  247.     }
  248. }
  249. /*** end of unshadow_window ***/
  250.  
  251. /*** END OF FILE ***/